package leetCode;

import java.util.Arrays;
import java.util.Objects;
import java.util.Scanner;

/*

给你一个 m x n 的矩阵 board ，由若干字符 'X' 和 'O' ，找到所有被 'X' 围绕的区域，
并将这些区域里所有的 'O' 用 'X' 填充。

4
4
XXXX
XOOX
XXOX
XOXX


3
3
OOO
OOO
OOO


5
5
XXXXX
XOXOO
XXOXO
XOXOO
OOOOO


5
5
XXXXX
XOXOO
XXOOO
XOOOO
OOOOO



 */

public class Solution130 {
    
    public void solve(char[][] board) {
        int m = board.length;
        int n = board[0].length;
        boolean[][] visited = new boolean[m][n];
        for (int i = 0; i < m; i++) {
            if (board[i][0] == 'O' && !visited[i][0]) {
                visited[i][0] = true;
                findO(i, 0, board, visited);
                
            }
            if (board[i][n-1] == 'O' && !visited[i][n-1]) {
                visited[i][n-1] = true;
                findO(i, n-1, board, visited);
            }
        }
        for (int i = 0; i < n; i++) {
            if (board[0][i] == 'O' && !visited[0][i]) {
                visited[0][i] = true;
                findO(0, i, board, visited);
            }
            if (board[m-1][i] == 'O' && !visited[m-1][i]) {
                visited[m-1][i] = true;
                findO(m-1, i, board, visited);
            }
        }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (board[i][j] == 'O' && !visited[i][j]) {
                    board[i][j] = 'X';
                }
            }
        }
    }

    private void findO(int i, int j, char[][] board, boolean[][] visited) {
        int m = board.length;
        int n = board[0].length;
        if (i - 1 >= 0 && board[i-1][j] == 'O' && !visited[i-1][j]) {
            visited[i-1][j] = true;
            findO(i-1, j, board, visited);
        }
        if (i + 1 < m && board[i+1][j] == 'O' && !visited[i+1][j]) {
            visited[i+1][j] = true;
            findO(i+1, j, board, visited);
        }
        if (j - 1 >= 0 && board[i][j-1] == 'O' && !visited[i][j-1]) {
            visited[i][j-1] = true;
            findO(i, j-1, board, visited);
        }
        if (j + 1 < n && board[i][j+1] == 'O' && !visited[i][j+1]) {
            visited[i][j+1] = true;
            findO(i, j+1, board, visited);
        }
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int m = scan.nextInt();
        int n = scan.nextInt();
        char[][] board = new char[m][n];
        for (int i = 0; i < m; i++) {
            board[i] = scan.next().toCharArray();
            Arrays.toString(board[i]);
        }
        Solution130 s = new Solution130();
        s.solve(board);
        for (int i = 0; i < m; i++) {
            System.out.println(Arrays.toString(board[i]));
        }
    }

}
class IndexIJ {
    public int i, j;
    public IndexIJ(int i, int j) {
        this.i = i;
        this.j = j;
    }
    @Override
    public String toString() {       
        return "(" + i + ", " + j + ")";
    }
    @Override
    public int hashCode() {
        return Objects.hash(i, j);
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        IndexIJ other = (IndexIJ) obj;
        return i == other.i && j == other.j;
    }
    
}
